package com.wang.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

public class StateMachine<T, A> {
    private T context;
    private int state;
    // 状态转移方程
    private Map<Integer, BiFunction<T, A, Integer>> transfer = new HashMap<>();

    public StateMachine(T context, int state, Map<Integer, BiFunction<T, A, Integer>> transfer) {
        this.context = context;
        this.state = state;
        this.transfer = transfer;
    }

    public StateMachine<T, A> transfer(A action) {
        this.state = transfer.get(state).apply(context, action);
        return this;
    }
}
